package com.zz.inventory.service.impl;

import java.util.List;
import com.ruoyi.common.core.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.ruoyi.common.core.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.zz.inventory.domain.SysUserRole;
import com.zz.inventory.mapper.SysUserMapper;
import com.zz.inventory.domain.SysUser;
import com.zz.inventory.service.ISysUserService;

/**
 * 用户信息Service业务层处理
 * 
 * @author zz
 * @date 2025-07-02
 */
@Service
public class SysUserServiceImpl implements ISysUserService 
{
    @Autowired
    private SysUserMapper sysUserMapper;

    /**
     * 查询用户信息
     * 
     * @param userId 用户信息主键
     * @return 用户信息
     */
    @Override
    public SysUser selectSysUserByUserId(Long userId)
    {
        return sysUserMapper.selectSysUserByUserId(userId);
    }

    /**
     * 查询用户信息列表
     * 
     * @param sysUser 用户信息
     * @return 用户信息
     */
    @Override
    public List<SysUser> selectSysUserList(SysUser sysUser)
    {
        return sysUserMapper.selectSysUserList(sysUser);
    }

    /**
     * 新增用户信息
     * 
     * @param sysUser 用户信息
     * @return 结果
     */
    @Transactional
    @Override
    public int insertSysUser(SysUser sysUser)
    {
        sysUser.setCreateTime(DateUtils.getNowDate());
        int rows = sysUserMapper.insertSysUser(sysUser);
        insertSysUserRole(sysUser);
        return rows;
    }

    /**
     * 修改用户信息
     * 
     * @param sysUser 用户信息
     * @return 结果
     */
    @Transactional
    @Override
    public int updateSysUser(SysUser sysUser)
    {
        sysUser.setUpdateTime(DateUtils.getNowDate());
        sysUserMapper.deleteSysUserRoleByUserId(sysUser.getUserId());
        insertSysUserRole(sysUser);
        return sysUserMapper.updateSysUser(sysUser);
    }

    /**
     * 批量删除用户信息
     * 
     * @param userIds 需要删除的用户信息主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteSysUserByUserIds(Long[] userIds)
    {
        sysUserMapper.deleteSysUserRoleByUserIds(userIds);
        return sysUserMapper.deleteSysUserByUserIds(userIds);
    }

    /**
     * 删除用户信息信息
     * 
     * @param userId 用户信息主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteSysUserByUserId(Long userId)
    {
        sysUserMapper.deleteSysUserRoleByUserId(userId);
        return sysUserMapper.deleteSysUserByUserId(userId);
    }

    @Override
    public List<SysUser> selectUsersBySchoolId(Long schoolId) {
        return sysUserMapper.selectUsersBySchoolId(schoolId);
    }

    /**
     * 新增用户和角色关联信息
     * 
     * @param sysUser 用户信息对象
     */
    public void insertSysUserRole(SysUser sysUser)
    {
        List<SysUserRole> sysUserRoleList = sysUser.getSysUserRoleList();
        Long userId = sysUser.getUserId();
        if (StringUtils.isNotNull(sysUserRoleList))
        {
            List<SysUserRole> list = new ArrayList<SysUserRole>();
            for (SysUserRole sysUserRole : sysUserRoleList)
            {
                sysUserRole.setUserId(userId);
                list.add(sysUserRole);
            }
            if (list.size() > 0)
            {
                sysUserMapper.batchSysUserRole(list);
            }
        }
    }
}
